

#include "Losu_Std.h"
#include<iostream>
#include<cmath>
#include<string>
#include<ctime>
#include<thread>
#include<vector>
using namespace std;
extern "C"{
extern LNI_return rt;
extern string rtstr;


LNI_return losu_math_abs(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    switch (a.type[3]){
        case ELS_TYPE_NUMBER:
            rt.num = a.num[3];
            if(a.num[3]<0) 
                rt.num = -a.num[3];
            break;
        case ELS_TYPE_STRING:
            rt.num = atof(a.str[3]);
            if(rt.num<0)
                rt.num = -rt.num;
            break;
    }
    return rt;
}
LNI_return losu_math_log(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    double s,n;
    switch (a.type[3]){
        case ELS_TYPE_NUMBER:
            s = a.num[3];
            break;
        case ELS_TYPE_STRING:
            s = atof(a.str[3]);
            break;
    }
    switch (a.type[4]){
        case ELS_TYPE_NUMBER:
            s = a.num[4];
            break;
        case ELS_TYPE_STRING:
            s = atof(a.str[4]);
            break;
    }
    rt.num = log(s/n);
    return rt;
}
LNI_return losu_math_pow(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    double s,n;
    switch (a.type[3]){
        case ELS_TYPE_NUMBER:
            s = a.num[3];
            break;
        case ELS_TYPE_STRING:
            s = atof(a.str[3]);
            break;
    }
    switch (a.type[4]){
        case ELS_TYPE_NUMBER:
            s = a.num[4];
            break;
        case ELS_TYPE_STRING:
            s = atof(a.str[4]);
            break;
    }
    rt.num = pow(s,n);
    return rt;
}
LNI_return losu_math_factorial(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    double s;
    switch (a.type[3]){
        case ELS_TYPE_NUMBER:
            s = a.num[3];
            break;
        case ELS_TYPE_STRING:
            s = atof(a.str[3]);
            break;
    }
    double sum=1;
	for(int i=1;i<=(int)s;i++){
		sum*=i;
	}
    rt.num = sum;
    return rt;
}
LNI_return losu_math_randint(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    int Max,Min;
    switch (a.type[3]){
        case ELS_TYPE_NUMBER:
            Min = (int)a.num[3];
            break;
        case ELS_TYPE_STRING:
            Min = (int)atof(a.str[3]);
            break;
    }
    switch (a.type[4]){
        case ELS_TYPE_NUMBER:
            Max = (int)a.num[4];
            break;
        case ELS_TYPE_STRING:
            Max = (int)atof(a.str[4]);
            break;
    };
    srand(clock());  
	rt.num = rand()%(int)(Max-Min+1)+Min;
    return rt;
}
}